import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:rebase_project/res/colors.dart';
import 'package:caose/caose.dart' show dip,sp;

import 'package:caose/caose.dart' show BaseState,LoadingDialogMixin;

class CustomWidthCupertinoDialog extends CupertinoAlertDialog{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return _CustomWidthCupertinoDialog(
        placeholder, tip, tag, callback1, callback2);
  }

  String placeholder;
  String tip;
  String tag;
  DialogCallback callback1;
  DialogCallback callback2;

  CustomWidthCupertinoDialog(
      this.tip, this.placeholder, this.tag, this.callback1, this.callback2);
}

class _CustomWidthCupertinoDialog extends StatefulWidget {
  _CustomWidthCupertinoDialog(
      this.tip, this.placeholder, this.tag, this.callback1, this.callback2);

  String placeholder;
  String tip;
  String tag;
  DialogCallback callback1;
  DialogCallback callback2;

  @override
  _CustomWidthState createState() =>
      _CustomWidthState(placeholder, tip, tag, callback1, callback2);
}

class _CustomWidthState extends BaseState<_CustomWidthCupertinoDialog> with LoadingDialogMixin {
  _CustomWidthState(
      this.tip, this.placeholder, this.tag, this.callback1, this.callback2);

  String placeholder;
  String tip;
  String tag;
  String tag2="0/16";
  String content;
  DialogCallback callback1;
  DialogCallback callback2;

  @override
  void initData() {
  }

  @override
  Widget initView(BuildContext context) {
    // TODO: implement initView
    return Center(
      child: ClipRRect(
          borderRadius: BorderRadius.all(Radius.circular(5)),
          child: Container(
            width: dip(325),
            height: dip(205),
            color: CupertinoColors.white,
            child: Column(
              children: <Widget>[
                Padding(
                    padding: EdgeInsets.symmetric(vertical:dip(25)),
                    child: Center(
                        child: Text(
                      tip,
                      style: TextStyle(
                          fontWeight: FontWeight.w400,
                          fontSize: sp(17),
                          color: CupertinoColors.black),
                    ))),
                Padding(
                  padding: EdgeInsets.symmetric(horizontal: dip(30)),
                  child: CupertinoTextField(
                    decoration: BoxDecoration(
                      color: Color(0xfff7f7f6),
                      borderRadius: BorderRadius.all(Radius.circular(5)),
                    ),
                    keyboardType: tag!=null ? TextInputType.number:null,
                    padding: EdgeInsets.all(dip(15)),
                    style:
                        TextStyle(fontSize: sp(14), color: CupertinoColors.black),
                    placeholder: placeholder,
                    maxLength: tag==null ? 16:3,
                    onChanged: (value) {
                      content=value;
                      if(tag==null)
                      setState(() {
                        tag2="${value.length}/16";
                      });
                    },
                  ),
                ),
                Padding(
                    padding: EdgeInsets.symmetric(horizontal: dip(30), vertical: dip(5)),
                    child: Align(
                      alignment: Alignment.topRight,
                      child: Text(
                        tag ?? tag2,
                        style: TextStyle(color: Color(0xff555555), fontSize: sp(14)),
                      ),
                    )),
                Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: <Widget>[
                    CupertinoButton(
                        child: Text(
                          "取消",
                          style: TextStyle(fontSize: sp(15), color: Color(0xff555555)),
                        ),
                        pressedOpacity: 0.3,
                        onPressed: () => callback2(content,context)),
                    CupertinoButton(
                        padding: EdgeInsets.fromLTRB(dip(15),dip(0),dip(30),dip(0)),
                        child: Text(
                          "确定",
                          style: TextStyle(fontSize: sp(15), color: c7283f7),
                        ),
                        pressedOpacity: 0.3,
                        onPressed: () {
                          if(content==null){
                            showToast(placeholder);
                            return;
                          }
                          callback1(content,context);
                        }),
                  ],
                ),
              ],
            ),
          )),
    );
  }
}

typedef DialogCallback = void Function(String content,BuildContext context);
